﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.web;
using umbraco.blog4.spam;

namespace umbraco.blog4.plugins.dashboard
{
    public partial class CommentModeration : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void BindData()
        {
            rptComments.DataBind();
        }

        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null) { return 0; } else { return (int)o; }
            }
            set { this.ViewState["_CurrentPage"] = value; }
        }

        private int? GetId(LinkButton button)
        {
            if (button == null)
                return null;
            return Convert.ToInt32(button.CommandArgument);
        }

        private int? GetId(Label label)
        {
            if (label == null)
                return null;
            int id = 0;
            if (int.TryParse(label.Text, out id))
                return id;
            else
                return null;
        }

        private Document SelectedComment(int? id){
            if (!id.HasValue)
                return null;

            var doc = new Document(id.Value);
            if (BlogLibrary.TypeAliasInherits(Comment.DocumentTypeName, doc.ContentType.Alias))
                return doc;

            return null;
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DeleteComment(GetId(sender as LinkButton));
            BindData();
        }

        // This is potentially dangerous as it will delete the comment and any child comments.
        private void DeleteComment(int? id)
        {
            var doc = SelectedComment(id);

            if (doc != null)
                doc.delete();
        }

        protected void btnToggleState_Click(object sender, EventArgs e)
        {
            var button = sender as LinkButton;
            var doc = SelectedComment(GetId(button));
            if (doc == null)
                return;

            var comment = new Comment(doc);
            comment.IsSpam = button.CommandName != "True";

            SpamChecker checker = Config.GetChecker();
            if (checker != null)
            {
                if (!comment.IsSpam)
                    checker.MarkAsHam(comment.Id, comment.Name, comment.Email, comment.Website, comment.Text);
                else
                    checker.MarkAsSpam(comment.Id, comment.Name, comment.Email, comment.Website, comment.Text);
            }

            BindData();
        }

        protected void btnTogglePublishedState_Click(object sender, EventArgs e)
        {
            var button = sender as LinkButton;
            var doc = SelectedComment(GetId(button));
            if (doc == null)
                return;

            var comment = new Comment(doc);
            if (comment.IsSpam)
                return;

            if (comment.IsPublished)
                comment.UnPublish();
            else
                comment.Publish();

            BindData();
        }

        public string GetPageDetails(object nodeid)
        {
            int id;
            if (int.TryParse(nodeid.ToString(), out id))
            {
                try
                {
                    Document page = new Document(id);
                    return string.Format(
                        "<a href='{0}' target='_blank'>{1}</a>",
                        umbraco.library.NiceUrl(page.Id), page.Text);
                }
                catch { return string.Empty; }
            }
            else
            {
                return string.Empty;
            }
        }

        public string GetToggleStateText(object spam)
        {
            bool isspam;
            if (bool.TryParse(spam.ToString(), out isspam))
            {
                if (isspam)
                    return "Mark as ham";
                else
                    return "Mark as spam";
            }
            else
            {
                return string.Empty;
            }
        }

        public string GetTogglePublishedStateText(object published)
        {
            bool isPublished;
            if (bool.TryParse(published.ToString(), out isPublished))
            {
                if (isPublished)
                    return "Hide";
                else
                    return "Make Visible";
            }
            else
            {
                return string.Empty;
            }
        }

        protected void btnApproved_Click(object sender, EventArgs e)
        {
            btnApproved.Enabled = false;
            btnSpam.Enabled = true;
            btnAll.Enabled = true;

            commentsObjectDataSource.SelectParameters["filter"].DefaultValue = "ham";

            CurrentPage = 0;
            BindData();
        }

        protected void btnSpam_Click(object sender, EventArgs e)
        {
            btnApproved.Enabled = true;
            btnSpam.Enabled = false;
            btnAll.Enabled = true;

            commentsObjectDataSource.SelectParameters["filter"].DefaultValue = "spam";

            CurrentPage = 0;
            BindData();
        }

        protected void btnAll_Click(object sender, EventArgs e)
        {
            btnApproved.Enabled = true;
            btnSpam.Enabled = true;
            btnAll.Enabled = false;

            commentsObjectDataSource.SelectParameters["filter"].DefaultValue = "";

            CurrentPage = 0;
            BindData();
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in rptComments.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("cbSelectComment");

                if (chk.Checked)
                {
                    DeleteComment(Convert.ToInt32(((Label)item.FindControl("lblID")).Text));
                }

            }

            BindData();
        }

        protected void btnHideAllSelected_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in rptComments.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("cbSelectComment");

                if (chk.Checked)
                {
                    var doc = SelectedComment(GetId(item.FindControl("lblID") as Label));
                    if (doc == null)
                        return;

                    var comment = new Comment(doc);
                    if (comment.IsSpam)
                        continue;

                    comment.UnPublish();
                }

            }

            BindData();
        }

        protected void btnUnHideAllSelected_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in rptComments.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("cbSelectComment");

                if (chk.Checked)
                {
                    var doc = SelectedComment(GetId(item.FindControl("lblID") as Label));
                    if (doc == null)
                        continue;

                    var comment = new Comment(doc);
                    if (comment.IsSpam)
                        continue;

                    comment.Publish();
                }

            }

            BindData();
        }

    }
}